<template>
  <div class="pagination">
    <button @click="goToFirstPage" :disabled="currentPage === 1">首页</button>
    <button @click="goToPreviousPage" :disabled="currentPage === 1">上一页</button>

    <span class="page-info">{{ currentPage }} / {{ totalPages }}</span>

    <button @click="goToNextPage" :disabled="currentPage === totalPages">下一页</button>
    <button @click="goToLastPage" :disabled="currentPage === totalPages">尾页</button>

    <input type="number" v-model="jumpPage" min="1" :max="totalPages"/>
    <button @click="jumpToPage">跳转</button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: {
      type: Number,
      required: true
    },
    totalPages: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      jumpPage: this.currentPage
    };
  },
  methods: {
    goToFirstPage() {
      this.$emit('page-changed', 1);
    },
    goToPreviousPage() {
      this.$emit('page-changed', this.currentPage - 1);
    },
    goToNextPage() {
      this.$emit('page-changed', this.currentPage + 1);
    },
    goToLastPage() {
      this.$emit('page-changed', this.totalPages);
    },
    jumpToPage() {
      if (this.jumpPage >= 1 && this.jumpPage <= this.totalPages) {
        this.$emit('page-changed', this.jumpPage);
      }
    }
  }
};
</script>

<style scoped lang="scss">
.pagination {
  display: flex;
  align-items: center;
  gap: 10px;
}

button {
  padding: 5px 10px;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.page-info {
  margin: 0 10px;
}

input {
  width: 50px;
  padding: 5px;
}
</style>
